<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use EasyWeChat\Foundation\Application;
use DB;
class CharttwoController extends Controller
{

  public $app = null;
  public $did;
  public $vers;
  public function __construct(){
    $options = [
      'debug'  => true,
      'app_id' => 'wx61a5ce5230a32e92',
      'secret' => '14ba21479bce9ff60ba93185cbc5e4bb',
      'token'  => 'johntest',
      // 'aes_key' => null, // 可选
      'log' => [
          'level' => 'debug',
          'file'  => '/tmp/easywechat.log', // XXX: 绝对路径！！！！
      ],
      'oauth' => [
         'scopes'   => ['snsapi_base'],
         'callback' => '/nnlogin',
       ],
      'guzzle'=>[
        'timeout'=>5.0,
        'verify'=>false,
      ]
      //...
    ];
    $this->app = new Application($options);
    //if ( isset( $_GET['did']  )  )
    {
    }    
  }
    public function nnlogin(Request $req){
        $oauth = $this->app->oauth;
        $user = $oauth->user();
        $req->session()->put('user', $user);
        //return redirect('/center');
	return $this->nntiao($req , $user);
    }
    public function nncenter(Request $req){
            $oauth = $this->app->oauth;
	if( !empty( $_GET['did'] ) )
	{
	    $req->session() -> put ( 'did' , $_GET['did'] );
         } else{  //return redirect('login');
	    $req ->session()->put( 'did' , '没有did' );
	}
	return $oauth->redirect();
    }

    public function nnlogout(Request $req){
        $req->session()->forget('user');
        return $this->redirect('/nncenter');
    }
    public function nntiao($req , $id){
	
	dd( $req->session() ->get('did'));
      	$open = $id['id'];
      	$userService = $this->app->user;
        $userer = $userService->get($open);
        $name = $userer->nickname;
        $sex = $userer->sex;//   1是男 2是女
	//if( $this->get_did != ''  )
	{
		dd( $this->did );
	}
	return redirect("http://www.laoyou1.me/chart1.2/index.php?open=$open");
	//return redirect("http://baidu.com");
    }
    public function https2_request($url,$data = null){
    	$curl = curl_init();
    	curl_setopt($curl, CURLOPT_URL, $url);
    	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    	if (!empty($data)){
        	curl_setopt($curl, CURLOPT_POST, 1);
        	curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
   	 }
    	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    	$output = curl_exec($curl);
    	curl_close($curl);
    	return $output;

    }

}
